package com.top.art.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.top.art.entity.ExaminationPaper;
import com.top.art.entity.ExamineeInfo;
import com.top.art.entity.StudioInfo;
import com.top.art.mapper.ExaminationPaperMapper;
import com.top.art.service.IExaminationInfoService;
import com.top.art.service.IExamineeInfoService;
import com.top.art.service.IPaperService;
import com.top.art.service.IStudioInfoService;
import com.top.art.utils.AliyunOssUtil;
import com.top.art.vo.PaperFileVO;
import lombok.extern.slf4j.Slf4j;
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.model.enums.CompressionLevel;
import net.lingala.zip4j.model.enums.CompressionMethod;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.tika.mime.MimeTypeException;
import org.apache.tika.mime.MimeTypes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: huangyixin
 * @create: 2020/10/11 10:33
 **/
@Slf4j
@Service
public class PaperServiceImpl implements IPaperService {

    @Autowired
    private IExaminationInfoService iExaminationInfoService;

    @Autowired
    private IExamineeInfoService iExamineeInfoService;

    @Autowired
    private ExaminationPaperMapper examinationPaperMapper;

    @Autowired
    private AliyunOssUtil aliyunOssUtil;

    @Autowired
    private IStudioInfoService iStudioInfoService;

    @Override
    public File downloadPaper(String studioCode) throws IOException {
        StudioInfo studioInfo = iStudioInfoService.getOne(Wrappers.<StudioInfo>lambdaQuery().eq(StudioInfo::getStudioCode, studioCode));
        List<ExamineeInfo> examinees = iExamineeInfoService.list(Wrappers.<ExamineeInfo>lambdaQuery().eq(ExamineeInfo::getStudioCode, studioCode));
        Map<String, ExamineeInfo> examineeInfoMap = examinees.stream().collect(Collectors.toMap(ExamineeInfo::getAdmissionTicketCode, i -> i));
        List<ExaminationPaper> examinationPapers = examinationPaperMapper.selectByStudioCode(studioCode);
        File colorFile = new File("色彩");
        colorFile.mkdir();
        File writingFile = new File("速写");
        writingFile.mkdir();
        File sketchFile = new File("素描");
        sketchFile.mkdir();
        File composeFile = new File("结构");
        composeFile.mkdir();
        for (ExaminationPaper paper : examinationPapers) {
            ExamineeInfo examineeInfo = examineeInfoMap.getOrDefault(paper.getAdmissionTicketCode(), null);
            if (examineeInfo != null) {
                String objectName = paper.getImg();
                objectName = objectName.substring(objectName.lastIndexOf("paper"));
                PaperFileVO paperFileVO = aliyunOssUtil.downloadFile(objectName);
                File tmp = new File(examineeInfo.getAdmissionTicketCode() + examineeInfo.getExamineeName() + getExtension(paperFileVO.getContentType()));
                        paperFileVO.getFile().renameTo(tmp);
                if (StringUtils.equals(paper.getCourse(), "色彩")) {
                    FileUtils.moveFileToDirectory(tmp, colorFile, false);
                } else if (StringUtils.equals(paper.getCourse(), "速写")) {
                    FileUtils.moveFileToDirectory(tmp, writingFile, false);
                } else if (StringUtils.equals(paper.getCourse(), "素描")) {
                    FileUtils.moveFileToDirectory(tmp, sketchFile, false);
                } else if (StringUtils.equals(paper.getCourse(), "结构")) {
                    FileUtils.moveFileToDirectory(tmp, composeFile, false);
                }
            }
        }
        File file = new File(studioInfo.getStudioName());
        file.mkdir();
        FileUtils.moveDirectoryToDirectory(colorFile, file, false);
        FileUtils.moveDirectoryToDirectory(writingFile, file, false);
        FileUtils.moveDirectoryToDirectory(sketchFile, file, false);
        FileUtils.moveDirectoryToDirectory(composeFile, file, false);
        return zipFile(file).getFile();
    }

    private ZipFile zipFile(File file) {
        String zipPath = file.getName().concat(".zip");
        ZipParameters parameters = new ZipParameters();
        parameters.setCompressionMethod(CompressionMethod.DEFLATE);
        parameters.setCompressionLevel(CompressionLevel.NORMAL);
        ZipFile zipFile = new ZipFile(zipPath);
        try {
            if (file.isFile()) {
                zipFile.addFile(file, parameters);
            } else if (file.isDirectory()) {
                zipFile.addFolder(file, parameters);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        FileUtils.deleteQuietly(file);
        return zipFile;
    }

    private String getExtension(String contentType){
        MimeTypes allTypes = MimeTypes.getDefaultMimeTypes();
        try {
            return allTypes.forName(contentType).getExtension();
        } catch (MimeTypeException e) {
            return ".jpg";
        }
    }
}
